#include "trunner.h"

NodeTRunner::NodeTRunner(Node *parent, NodeTRunnerParam params) :
    Node(parent)
{
    const_multiplexer = params.multiplexer;
    const_num_of_main_memory = params.num_of_main_memory;
    reg_is_running = false;
    port_nano_op_receiver = new ChannelTranOpFabricReceiver(this);
    List<ChannelLoadReqFabricSender *> tmp_885031040;
    for(int temp_885062344=0; temp_885062344<params.num_of_main_memory; temp_885062344+=1)
    {
        tmp_885031040.append(new ChannelLoadReqFabricSender(this));
    }
    port_memory_ports = tmp_885031040;
}

NodeTaskList NodeTRunner::tasks() const
{
    NodeTaskList list;
    list.append(static_cast<NodeTask>(&NodeTRunner::task_fetch_op));
    list.append(static_cast<NodeTask>(&NodeTRunner::task_dispatch_execution));
    return list;
}

void NodeTRunner::task_fetch_op()
{
    if((reg_is_running)||(port_nano_op_receiver->Bus->is_any_empty()))
    {
        return;
    }
    LaneChannelTranOpFabricBusPack ret_885060328 = port_nano_op_receiver->Bus->get();
    reg_nano_op = ret_885060328.nano_op;
    reg_len = 0;
    reg_src_addr = reg_nano_op.src_addr;
    reg_dst_addr = reg_nano_op.dst_addr;
    reg_is_running = true;
}

void NodeTRunner::task_dispatch_execution()
{
    for(int temp_885060888=0; temp_885060888<const_multiplexer; temp_885060888+=1)
    {
        if(!(reg_is_running))
        {
            return;
        }
        for(int ii=0; ii<const_num_of_main_memory; ii+=1)
        {
            if(port_memory_ports[ii]->Bus->is_any_full())
            {
                continue;
            }
            if(((reg_len)==(reg_nano_op.len_limit))||((reg_len)>(reg_nano_op.len_limit)))
            {
                reg_is_running = false;
                return;
            }
            port_memory_ports[ii]->Bus->put(reg_src_addr, reg_nano_op.dst_peer_id, reg_dst_addr, reg_nano_op.dst_sb_addr, reg_nano_op.dst_ain_addr, reg_nano_op.dst_clear);
            reg_src_addr += reg_nano_op.src_stride;
            reg_dst_addr += reg_nano_op.dst_stride;
            reg_len += 1;
        }
    }
}